<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-11 09:14
 *
 * Version: 2.0
 */

namespace App\Modules\Employee\Repositories;


use App\Models\UserCoin;
use App\Modules\Employee\Validators\UserCoinValidator;
use Flower\Core\Repository\Eloquent\BaseRepository;

class UserCoinRepository extends BaseRepository
{
	public function model()
	{
		return UserCoin::class;
	}

	public function validator()
	{
		return UserCoinValidator::class;
	}

	/**
	 * 查找某用户某天的积分和
	 * @param $requestData
	 * @return mixed
	 */
	public function getUserCoinDay($requestData){
		$date = empty($requestData['date']) ? date("Y-m-d",time()) : $requestData['date'];
		$map = [
			'user_id' => $requestData['user_id'],
			'store_id' => $requestData['store_id'],
		];
		return $this->model->where($map)->whereDate('created_at',$date)->get()->sum('coin');
	}

	/**
	 * 新增积分记录
	 * @param $userId           剪发师Id
	 * @param $storeId          店铺Id
	 * @param float $coin       积分
	 * @param int $source       来源
	 * @param int $linkId       关联Id
	 * @param string $purpose   用途
	 * @param string $date      积分日期
	 * @return mixed
	 */
	public function add($userId,$storeId,$coin = 0.0,$source = 0,$linkId = 0,$purpose='', $date =''){
		$data = [
			'user_id'       => $userId,
			'store_id'      => $storeId,
			'coin'          => empty($coin) ? 0.1 : $coin,
			'coin_date'     => empty($date) ? time() : strtotime($date),
			'source'        => $source,
			'link_id'       => $linkId,
			'purpose'       => $purpose

		];
		return $this->create($data);
	}
}